﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using Entity;
using Utilities;


namespace DAL
{
    public class OutGalleryDataAccess : FreshCmsAccessBase
    {
        #region code CMS

        /// <summary>
        /// Lấy list ảnh gallery cho sản phẩm
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<OutGallery> GetOutGalleryListByProId(int id)
        {
            try
            {
                const string spName = "spi_OutGallery_GetList_ByProId";

                var sqlParams = new[]
                                    {
										new SqlParameter("@ProId", id)
                                    };
                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                var OutGalleryList = DbHelper.ExecuteSqlList<OutGallery>(sqlCommand);
                return OutGalleryList;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return new List<OutGallery>();
            }
        }

        /// <summary>
        /// Lấy list ảnh gallery cho tin tức
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<OutGallery> GetOutGalleryListByNewId(int id)
        {
            try
            {
                const string spName = "spi_OutGallery_GetList_ByNewId";

                var sqlParams = new[]
                                    {
										new SqlParameter("@NewId", id)
                                    };
                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                var OutGalleryList = DbHelper.ExecuteSqlList<OutGallery>(sqlCommand);
                return OutGalleryList;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return new List<OutGallery>();
            }
        }

        public int InsertOutGallery(OutGallery OutGallery)
        {
            try
            {
                const string spName = "spi_OutGallery_Insert";
                var sqlParams = new[]
                                    {
                                        new SqlParameter("@ProId", OutGallery.ProId),
                                        new SqlParameter("@NewId", OutGallery.NewId),
                                        new SqlParameter("@GalleryImg", OutGallery.GalleryImg),
                                        new SqlParameter("@GalleryThumb", OutGallery.GalleryThumb),
                                        new SqlParameter("@GalleryId", OutGallery.GalleryId),
                                    };

                SqlCommand sqlCommand = new SqlCommand(spName);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.CommandText = spName;
                sqlCommand.Parameters.AddRange(sqlParams);
                sqlCommand.Parameters["@GalleryId"].Direction = ParameterDirection.Output;
                DbHelper.ExecuteNonQuery(sqlCommand);
                if (sqlCommand.Parameters["@GalleryId"].Value != null)
                {
                    return (int)sqlCommand.Parameters["@GalleryId"].Value;
                }

                return 0;


            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return -1;
            }
        }

        public int DeleteOutGallery(int galleryId)
        {
            try
            {
                const string spName = "spi_OutGallery_Delete";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@GalleryId", galleryId)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return -1;
            }
        }

        /// <summary>
        /// Xóa gallery được chọn
        /// </summary>
        /// <param name="lstGalleryId"></param>
        /// <returns></returns>
        public int DeleteMultiOutGallery(string lstGalleryId)
        {
            try
            {
                const string spName = "spi_OutGallery_DeleteMulti";
                var sqlParams = new[]
                                {
                                    new SqlParameter("@LstGalleryId", lstGalleryId)
                                };

                var returnVal = DbHelper.ExecuteInt32SP(spName, sqlParams);

                return returnVal;
            }
            catch (Exception exp)
            {
                LoggerCms.WriteLog(LoggerCms.LogType.Error, exp.ToString());

                return -1;
            }
        }


        #endregion
    }
}
